import { passMessage } from "@/request/index";
import Footer from "@/components/footer";
import Hearder from "@/components/header";
import { Button, Form, Input, message } from "antd";
import React from "react";
import css from "./index.module.scss";
const { TextArea } = Input;
const Mail: React.FC = (props: any) => {
  const [form] = Form.useForm();
  const onFinish = () => {
    const values = form.getFieldsValue();
    console.log(values);
    const { name, phone, text } = values;
    const time = localStorage.getItem("time");
    if (
      new Date().valueOf() - new Date(Number(time)).valueOf() >=
        60 * 60 * 1000 ||
      !time
    ) {
      if (!name || !phone || !text) {
        message.error("姓名，电话，内容均不能为空");
        return;
      }
      passMessage({
        phone,
        name,
        text,
      }).then((res: any) => {
        if (res.code === 0) {
          message.success("我已关注到你的留言");
          form.resetFields();
          localStorage.setItem("time", new Date().valueOf().toString());
        } else {
          message.error(res.msg || "提交失败");
        }
      });
    } else {
      message.error("一小时内只能提交一次");
    }
  };

  return (
    <div className={css.mail}>
      <Hearder />
      <div className={css.content}>
        <div className={css.title}>给我说点啥？</div>
        <div className={css.detail}>
          <Form
            name="basic"
            wrapperCol={{ span: 16 }}
            initialValues={{ remember: true }}
            autoComplete="off"
            form={form}
          >
            <Form.Item label="" name="name">
              <Input placeholder="姓名" />
            </Form.Item>

            <Form.Item label="" name="phone">
              <Input placeholder="联系电话" />
            </Form.Item>
            <Form.Item label="" name="text">
              <TextArea rows={4} placeholder="请留言......" />
            </Form.Item>

            <Form.Item wrapperCol={{ span: 16 }}>
              <Button
                type="primary"
                onClick={() => {
                  onFinish();
                }}
              >
                提 交
              </Button>
            </Form.Item>
          </Form>
        </div>
      </div>
      <Footer />
    </div>
  );
};

export default Mail;
